<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018/8/22 0022
 * Time: 16:07
 */
namespace app\admin\model;
use think\Db;

class UserModel
{
    /**获取后台user实例模型
     * @return $this
     */
    static public function getAdminUser(){
            return Db::view('admin_jurisdiction',true)->view('adminuser','*','admin_jurisdiction.id=adminuser.admin_jurisdiction','right');
    }

    /**添加用户
     * @param $data
     * @return int|string
     */
    static public function addAdminUser($data){
        return Db::name('adminuser')->insert($data);
    }

    /**删除后台用户
     * @param $field
     * @param null $op
     * @param null $condition
     * @return int
     */
    static public function delAdminUser($field, $op = null, $condition = null){
        $sul  = Db::connect();
        $user = $sul->name('adminuser')->where($field,$op,$condition)->find();
        if($user['admin_portrait']){
            if(is_file(config('FileRoot').$user['admin_portrait'])){
                unlink(config('FileRoot').$user['admin_portrait']);
            }
        }
        return $sul->name('adminuser')->where($field,$op,$condition)->delete();
    }

    /**更新后台用户
     * @param $data
     * @return int|string
     */
    static public function updateAdminUser($data){
        $sul = Db::connect();
        if(isset($data['id'])){
            $user = $sul->name('adminuser')->where('id',$data['id'])->find();
        }else{
            $user = $sul->name('adminuser')->where('adminuser',$data['adminuser'])->find();
        }
        if(isset($data['admin_portrait'])&&$data['admin_portrait']) {
            if ($user['admin_portrait']) {
                if (is_file(config('FileRoot') . $user['admin_portrait'])) {
                    unlink(config('FileRoot'). $user['admin_portrait']);
                }
            }
        }
        if(isset($data['id'])){
            return $sul->name('adminuser')->where('id',$data['id'])->update($data);
        }else{
            return $sul->name('adminuser')->where('adminuser',$data['adminuser'])->update($data);
        }
    }

    /**获取前台user实例模型
     * @return $this
     */
    static public function getIndexUser(){
        return Db::view('user',true)
            ->view('company','company_name,address,phone,charge','company.id=user.company_id','left')
            ->view('departmen_table','departmen_name,number','departmen_table.id=user.department_id','left')
            ->view('position_table','position_name,jurisdiction','position_table.id=user.post_id','left');
    }

    /**添加前台用户
     * @param $data
     * @return int|string
     */
    static public function addIndexUser($data){
        return Db::name('user')->insert($data);
    }

    /**删除前台用户
     * @param $field
     * @param null $op
     * @param null $condition
     * @return int
     */
    static public function delIndexUser($field, $op = null, $condition = null){
        $sul  = Db::connect();
        $user = $sul->name('user')->where($field,$op,$condition)->find();
        if($user['portrait']){
            if(is_file(config('FileRoot').$user['portrait'])){
                unlink(config('FileRoot').$user['portrait']);
            }
        }
        return $sul->name('user')->where($field,$op,$condition)->delete();
    }

    /**更新前台用户
     * @param $data
     * @return int|string
     */
    static public function updateIndexUser($data){
        $sul = Db::connect();
        if(isset($data['id'])){
            $user = $sul->name('user')->where('id',$data['id'])->find();
        }else{
            $user = $sul->name('user')->where('user',$data['user'])->find();
        }
        if(isset($data['portrait'])&&$data['portrait']){
            if(is_file(config('FileRoot').$user['portrait'])){
                unlink(config('FileRoot').$user['portrait']);
            }
        }
        if(isset($data['id'])){
            return $sul->name('user')->where('id',$data['id'])->update($data);
        }else{
            return $sul->name('user')->where('user',$data['user'])->update($data);
        }
    }
}